import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:gksz/political_Theory/Recommend/MessagePage/MessagePage.dart';
import 'package:gksz/political_Theory/Video/VideoPage.dart';
import '../Recommend/Recommend.dart';
import '../Study/Study.dart';
import '../Video/Video.dart';

Dio dio = new Dio();

class NavBar extends StatefulWidget{
  NavBar({Key key}) : super(key: key);
  _NavBarState createState() => _NavBarState();
}
class _NavBarState extends State<NavBar>
with SingleTickerProviderStateMixin{
  TabController _tabController;


  void initState() {
    super.initState();
    _tabController = new TabController(vsync: this, length: 3);
    getClipboardContents();
  }
    var copynews;
  var copyvideo;
  // var copytitle;
  getClipboardContents() async {// 访问剪贴板的内容。
    
    ClipboardData clipboardData = await Clipboard.getData(Clipboard.kTextPlain);
    String _copy = clipboardData.text.trim();
    if (_copy != null) {
      var lastindex=_copy.lastIndexOf('复')-4;
      var copytitle;
      if (lastindex>0) {
        copytitle=_copy.substring(0,lastindex);
      }else{
        copytitle=_copy;
      }
      print(copytitle);
      Response response_news = await dio.get(
          'http://xssw.gdit.edu.cn/sizheng/api/v1/news?query={"title":"${copytitle}"}');
      copynews = response_news.data;
      Response response_video = await dio.get(
          'http://xssw.gdit.edu.cn/sizheng/api/v1/video?query={"title":"${copytitle}"}');
      copyvideo = response_video.data;
      if ('${copynews}' != '[]') {
        showDialog<Null>(
          context: context,
          barrierDismissible: false,
          builder: (BuildContext context) {
            return new AlertDialog(
              contentPadding: EdgeInsets.all(5),
              content: new SingleChildScrollView(
                  child: Column(
                children: <Widget>[
                  Image.network('${copynews[0]['album']}'),
                  Container(
                    child: Text('${copynews[0]['title']}'),) ,
                  Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      InkWell(
                        child: Container(
                          width: 200,
                          height: 40,
                          // padding:EdgeInsets.all(10),
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(20),
                            color: Colors.red,
                          ),
                          child: Text(
                            '打开',
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 18.0,
                            ),
                          ),
                        ),
                        onTap: () {
                          Navigator.of(context).pop();
                          Navigator.of(context).push(MaterialPageRoute(
                                builder: (context) => MessagePage(msg:copynews[0])));
                        },
                      ),
                      IconButton(
                        iconSize: 30,
                        
                        icon: Icon(Icons.highlight_off,color: Colors.grey,),
                        onPressed: (){
                          Navigator.of(context).pop();
                        },
                      ),
                    ],
                  )
                ],
              )),
            );
          },
        );
      }else if('${copyvideo}' != '[]'){
        showDialog<Null>(
          context: context,
          barrierDismissible: false,
          builder: (BuildContext context) {
            return new AlertDialog(
              contentPadding: EdgeInsets.all(5),
              content: new SingleChildScrollView(
                  child: Column(
                children: <Widget>[
                  Image.network('${copyvideo[0]['album']}'),
                  Container(
                    padding: EdgeInsets.only(bottom: 10,top: 5),
                    child: Text('${copyvideo[0]['title']}'),) ,
                  Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      InkWell(
                        child: Container(
                          width: 200,
                          height: 40,
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(20),
                            color: Colors.red,
                          ),
                          child: Text(
                            '打开',
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 18.0,
                            ),
                          ),
                        ),
                        onTap: () {
                          Navigator.of(context).pop();
                          Navigator.of(context).push(MaterialPageRoute(
                                builder: (context) => VideoPage(msg:copyvideo[0])));
                        },
                      ),
                      IconButton(
                        iconSize: 30,
                        icon: Icon(Icons.highlight_off,color: Colors.grey,),
                        onPressed: (){
                          Navigator.of(context).pop();
                        },
                      ),
                    ],
                  )
                ],
              )),
            );
          },
        );
      }

    }
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: PreferredSize(child: AppBar(
        title: new TabBar(
          
          tabs: <Widget>[
            new Tab(text: '推荐',),
            new Tab(text: '学习',),
            new Tab(text: '视频',),
            
          ],
          
          indicatorColor: Colors.red[700],
          indicatorWeight: 5.0,
          indicatorSize: TabBarIndicatorSize.label,
          unselectedLabelColor: Colors.black,
          labelColor: Colors.red[700],
          controller: _tabController,
          labelStyle: TextStyle(fontSize: 18.0),
          
        ),
        backgroundColor: Colors.white,
        
      ),
      preferredSize: Size.fromHeight(40.0),
      ),
      
      
      body: new TabBarView(
        controller: _tabController,
        children: <Widget>[
          new Center(child: Recommend()),
          new Center(child: Study()),
          new Center(child: Video()),
        ],
      ),
    );
  }
}